<template>
  <div class="scroll-indicator">
    <!-- 滚动进度条 -->
    <div class="progress-bar">
      <div class="progress-fill" :style="{ height: `${scrollProgress}%` }"></div>
    </div>

    <!-- 返回顶部按钮 -->
    <button
      v-show="showBackToTop"
      class="scroll-btn back-to-top"
      @click="scrollToTop"
      title="返回顶部"
    >
      <span>↑</span>
    </button>

    <!-- 滚动到底部按钮 -->
    <button
      v-show="showScrollToBottom"
      class="scroll-btn scroll-to-bottom"
      @click="scrollToBottom"
      title="滚动到底部"
    >
      <span>↓</span>
    </button>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'

const scrollProgress = ref(0)
const showBackToTop = ref(false)
const showScrollToBottom = ref(false)

const updateScrollProgress = () => {
  const scrollTop = window.pageYOffset || document.documentElement.scrollTop
  const scrollHeight = document.documentElement.scrollHeight - window.innerHeight
  scrollProgress.value = scrollHeight > 0 ? (scrollTop / scrollHeight) * 100 : 0

  // 显示/隐藏返回顶部按钮
  showBackToTop.value = scrollTop > 300

  // 显示/隐藏滚动到底部按钮
  const isNearBottom = scrollTop + window.innerHeight >= scrollHeight - 100
  showScrollToBottom.value = !isNearBottom && scrollTop > 100
}

const scrollToTop = () => {
  window.scrollTo({
    top: 0,
    behavior: 'smooth',
  })
}

const scrollToBottom = () => {
  window.scrollTo({
    top: document.documentElement.scrollHeight,
    behavior: 'smooth',
  })
}

onMounted(() => {
  window.addEventListener('scroll', updateScrollProgress)
  updateScrollProgress()
})

onUnmounted(() => {
  window.removeEventListener('scroll', updateScrollProgress)
})
</script>

<style scoped>
.scroll-indicator {
  position: fixed;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.progress-bar {
  width: 3px;
  height: 120px;
  background: rgba(0, 0, 0, 0.1);
  border-radius: 2px;
  position: relative;
  overflow: hidden;
}

.progress-fill {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  background: linear-gradient(to top, #111111, #666666);
  border-radius: 2px;
  transition: height 0.1s ease;
  box-shadow: 0 0 10px rgba(17, 17, 17, 0.3);
}

.scroll-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  color: #111111;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
}

.scroll-btn:hover {
  background: #111111;
  color: #ffffff;
  transform: scale(1.1);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

.scroll-btn:active {
  transform: scale(0.95);
}

.back-to-top {
  opacity: 0;
  animation: fadeInUp 0.3s ease forwards;
}

.scroll-to-bottom {
  opacity: 0;
  animation: fadeInDown 0.3s ease forwards;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .scroll-indicator {
    right: 12px;
  }

  .scroll-btn {
    width: 36px;
    height: 36px;
    font-size: 14px;
  }
}
</style>
